﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;
using Ks.Fs.Core.Data.Provider;
using Ks.Fs.Entity;
using System.Data;
using Ks.Fs.Core.Data.SqlDataProvider.Mapper;

namespace Ks.Fs.Core.Data.SqlDataProvider
{
    public partial class SqlDataProvider : IDataProvider
    {
        
        public int SaveOrUpdateFile(File obj)
        {
            string query = "File_SaveOrUpdate";
            object _id = DataHelper.ExecuteScalar
                (
                query,obj.Id,obj.Filename,obj.FolderId,obj.Size,
                obj.LastModified,obj.Owner,obj.Path
                );
            if (_id != null) return Convert.ToInt32(_id);
            return -1;
        }

        public bool DeleteFile(int FileId)
        {
            string query = "File_Delete";
            int rowAffected = DataHelper.ExecuteNonQuery(query,FileId);
            if (rowAffected > 0) return true;
            return false;
        }

        public List<File> GetFiles(int FolderId,int SortBy, bool IsSortAscending, int PageSize, int PageIndex)
        {
            string direction = "ASC";
            if (!IsSortAscending)
            {
                direction = "DESC";
            }
            IDataReader reader = DataHelper.ExecuteReader("File_GetByVaryParam", FolderId, SortBy, direction, PageSize, PageIndex);
            FileMapper map = new FileMapper();
            return map.Map(reader);               
        }       

        public File GetFile(int FileId)
        {
            IDataReader reader = DataHelper.ExecuteReader("File_GetById", FileId);
            FileMapper map = new FileMapper();
            List<File> lst = map.Map(reader);
            if (lst.Count > 0) return lst[0];
            return null;
        }
    }
}

